package birdsong.example;

import birdsong.comm.ServerComm;
import birdsong.comm.ServerSideMessage;

/**
 * An example of a server side message filter that decorates a {@link birdsong.comm.ServerComm}. 
 * The message filter throws away Birdsong messages contain sensitive words. An array of 
 * sensitive words is provided at construction time.
 * 
 * <p>
 * Notice that the filter has a lot of limitations and is by no means complete, but it illustrates 
 * how filters can be built based on the Decorator pattern.
 * 
 * @author alc
 *
 */

public class MessageFilter implements ServerComm {

	private ServerComm decoratedServerComm;
	private String[]   sensitiveWords;
	
	/**
	 * Create the message filter.
	 * 
	 * @param decoratedServerComm the server comm interface to decorate
	 * @param sensitiveWords the words which the filter should check messages for. 
	 *         If a Birdsong message from a client matches any of the words in the array,
	 *         it is rejected.
	 */
	
	public MessageFilter(ServerComm decoratedServerComm, String[] sensitiveWords) {
		this.decoratedServerComm = decoratedServerComm; 
		this.sensitiveWords      = sensitiveWords;
	}
	
	@Override
	public void start() {
		decoratedServerComm.start();
	}

	@Override
	public ServerSideMessage getNextMessage() {
		
		boolean allow;
		ServerSideMessage message;
		do {
			allow   = true;
			message = decoratedServerComm.getNextMessage();
			if (message != null) 
			{
				if (message.getType() == ServerSideMessage.Type.BIRDSONG_MESSAGE) {
					int      index = 0;
					while (index < sensitiveWords.length && allow) {
						allow = !message.getPayload().contains(sensitiveWords[index++]);
						System.out.println("Checking message: " + message.getPayload() + " againts '" + sensitiveWords[index-1] + "'"  );
					}
					if (!allow)
						System.out.println("Message filtered");
				} 
			}			
		} while (!allow);

		
		return message;
	}

	@Override
	public boolean hasNextMessage() {
		return decoratedServerComm.hasNextMessage();
	}

	@Override
	public void sendBirdsongMessage(String fromNicename, String toNickname,
			String birdsongMessage) {
		decoratedServerComm.sendBirdsongMessage(fromNicename, toNickname, birdsongMessage);
	}

	@Override
	public void sendNewFollower(String toNickname, String newFollower) {
		decoratedServerComm.sendNewFollower(toNickname, newFollower);
	}

	@Override
	public void sendNewUnfollow(String toNickname, String unfollower) {
		decoratedServerComm.sendNewUnfollow(toNickname, unfollower);
		
	}

	@Override
	public void sendError(String toNickname, String error) {
		decoratedServerComm.sendError(toNickname, error);
	}

	@Override
	public void sendClientConnected(String toNickname, String connectedNickname) {
		decoratedServerComm.sendClientConnected(toNickname, connectedNickname);
	}

	@Override
	public void sendClientDisconnected(String toNickname,
			String disconnectedNickname) {
		decoratedServerComm.sendClientDisconnected(toNickname, disconnectedNickname);
		
	}

	@Override
	public boolean clientIsConnected(String nickname) {
		return decoratedServerComm.clientIsConnected(nickname);
	}

	@Override
	public void disconnectClient(String nickname) {
		decoratedServerComm.disconnectClient(nickname);
	}

}